/**
 * Project Name:scapp-web
 * File Name:HandEmployeeController.java
 * Package Name:com.sc.controller
 * Date:2018年7月4日上午9:47:36
 * Copyright (c) 2018, bluemobi All Rights Reserved.
 */

package com.sc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.pojo.BuyHoueSave;
import com.sc.pojo.Employee;
import com.sc.pojo.Search;
import com.sc.pojo.WorkLoad;
import com.sc.service.EmployeeService;

/**
 * Description: <br/>
 * Date: 2018年7月4日 上午9:47:36 <br/>
 * 
 * @author liutao
 * @version
 * @see
 */
@Controller
// @RequestMapping(value = "/HandEmployeeController")
public class HandEmployeeController {
    @Autowired
    private EmployeeService employeeService;

    private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(HandEmployeeController.class);

    @ResponseBody
    @RequestMapping("/queryGetMoneyAndCountAndAreaByYear")
    public Map<String, Object> queryGetMoneyAndCountAndAreaByYear() {
        System.out.println("进来");
        List<Employee> list = employeeService.queryGetMoneyAndCountAndAreaByYear();
        String[] ename = new String[list.size()];
        Float[] harea = new Float[list.size()];
        Float[] eprice = new Float[list.size()];
        Integer[] rank = new Integer[list.size()];
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            ename[i] = list.get(i).getEname();
            rank[i] = list.get(i).getRank();
            Set<BuyHoueSave> set = list.get(i).getSetBuyHouse();
            for (BuyHoueSave buyHoueSave : set) {
                harea[i] = buyHoueSave.getHouse().getHarea();
                eprice[i] = buyHoueSave.getHouse().getEprice();
            }
        }
        map.put("name", ename);
        map.put("harea", harea);
        map.put("eprice", eprice);
        map.put("rank", rank);
        return map;
    }

    @RequestMapping("/detailedpersonnel")
    public String selectAllEmployeeInfo(Model model) {
        List<Employee> list = employeeService.selectEmployeesAndOnjop();
        System.out.println("几个工作人员:" + list.size());
        model.addAttribute("list", list);
        return "sc-detailedpersonnel";

    }

    @RequestMapping("/yejisearch")
    public String yejisearch() {
        return "sc-yejisearch";
    }

    @ResponseBody
    @RequestMapping("/selectWorkloadByYear")
    public Map<String, Object> selectWorkloadByYear() {
        System.out.println("进来l");
        List<Employee> list = employeeService.selectWorkloadByYear();
        String[] ename = new String[list.size()];
        Integer[] wcompleteness = new Integer[list.size()];
        Integer[] wdonereportload = new Integer[list.size()];
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            ename[i] = list.get(i).getEname();

            Set<WorkLoad> set = list.get(i).getSet();

            for (WorkLoad workLoad : set) {
                wcompleteness[i] = workLoad.getWcompleteness();
                wdonereportload[i] = workLoad.getWdonereportload();
            }
        }
        map.put("name", ename);
        map.put("wcompleteness", wcompleteness);
        map.put("wdonereportload", wdonereportload);
        return map;
    }

    @RequestMapping("/yewusearch")
    public String yewusearch() {
        return "sc-yewusearch";
    }

    @ResponseBody
    @RequestMapping("/tt")
    public List<Employee> tt() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        // map.put("eid", "%" + 2 + "%");
        // map.put("ename", "%张%");
        map.put("sdate", "2018-06-06");
        System.out.println("爸爸来也");
        List<Employee> list = employeeService.queryGetMoneyAndCountAndAreaByYear(map);
        return list;

    }

    @ResponseBody
    @RequestMapping("/cc")
    public List<Employee> cc() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("eid", "%" + 1 + "%");
        // map.put("ename", "%张%");
        // map.put("sdate", "2018-06-06");
        System.out.println("大舅来也");
        List<Employee> list = employeeService.selectWorkloadByYear(map);
        return list;
    }

    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object> search(@RequestBody Search search) {
        System.out.println(search);
        HashMap<String, Object> hashmap = new HashMap<String, Object>();
        Integer eid = search.getEid();
        String ename = search.getEname();
        Date sdate = search.getSdate();
        Date edate = search.getEdate();
        System.out.println(search.getEname().length());
        if (eid != 0 || ename.trim().length() > 0 || sdate != null || edate != null) {
            if (eid != 0) {
                System.out.println("不为空:" + eid);
                hashmap.put("eid", "%" + eid + "%");
            }
            if (ename.trim().length() > 0) {
                hashmap.put("ename", "%" + ename + "%");
            }
            if (sdate != null) {
                hashmap.put("sdate", sdate);
            }
            if (edate != null) {
                hashmap.put("edate", edate);
            }
            List<Employee> list = employeeService.queryGetMoneyAndCountAndAreaByYear(hashmap);
            String[] name = new String[list.size()];
            Float[] harea = new Float[list.size()];
            Float[] eprice = new Float[list.size()];
            Integer[] rank = new Integer[list.size()];
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < list.size(); i++) {
                name[i] = list.get(i).getEname();
                rank[i] = list.get(i).getRank();
                Set<BuyHoueSave> set = list.get(i).getSetBuyHouse();
                for (BuyHoueSave buyHoueSave : set) {
                    harea[i] = buyHoueSave.getHouse().getHarea();
                    eprice[i] = buyHoueSave.getHouse().getEprice();
                }
            }
            map.put("name", name);
            map.put("harea", harea);
            map.put("eprice", eprice);
            map.put("rank", rank);
            System.out.println(map);
            return map;
        } else {
            List<Employee> list = employeeService.queryGetMoneyAndCountAndAreaByYear();
            String[] name = new String[list.size()];
            Float[] harea = new Float[list.size()];
            Float[] eprice = new Float[list.size()];
            Integer[] rank = new Integer[list.size()];
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < list.size(); i++) {
                name[i] = list.get(i).getEname();
                rank[i] = list.get(i).getRank();
                Set<BuyHoueSave> set = list.get(i).getSetBuyHouse();
                for (BuyHoueSave buyHoueSave : set) {
                    harea[i] = buyHoueSave.getHouse().getHarea();
                    eprice[i] = buyHoueSave.getHouse().getEprice();
                }
            }
            map.put("name", name);
            map.put("harea", harea);
            map.put("eprice", eprice);
            map.put("rank", rank);
            return map;
        }

    }

    @RequestMapping("/wxSearchStatic")
    public String wxSearchStatic(Model model, Integer eid) {
        LOG.info("eid:" + eid);
        model.addAttribute("eid", eid);
        return "sc-wx-performanceSearch";
    }

    @RequestMapping("/wxSearch")
    @ResponseBody
    public Map<String, Object> wxSearch(@RequestBody JSONObject params) {
        HashMap<String, Object> hashmap = new HashMap<String, Object>();
        System.out.println("不为空:" + params);
        Integer eid = params.getInt("eid");
        hashmap.put("eid", "%" + eid + "%");
        List<Employee> list = employeeService.queryGetMoneyAndCountAndAreaByYear(hashmap);
        System.out.println(list);
        String[] name = new String[list.size()];
        Float[] harea = new Float[list.size()];
        Float[] eprice = new Float[list.size()];
        Integer[] rank = new Integer[list.size()];
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            name[i] = list.get(i).getEname();
            rank[i] = list.get(i).getRank();
            Set<BuyHoueSave> set = list.get(i).getSetBuyHouse();
            for (BuyHoueSave buyHoueSave : set) {
                harea[i] = buyHoueSave.getHouse().getHarea();
                eprice[i] = buyHoueSave.getHouse().getEprice();
            }
        }
        map.put("name", name);
        map.put("harea", harea);
        map.put("eprice", eprice);
        map.put("rank", rank);
        System.out.println(map);
        return map;

    }

    @RequestMapping("/searchyewu")
    @ResponseBody
    public Map<String, Object> searchyewu(@RequestBody Search searchYeWu) {
        System.out.println(searchYeWu);
        HashMap<String, Object> hashmap = new HashMap<String, Object>();
        Integer eid = searchYeWu.getEid();
        String ename = searchYeWu.getEname();
        Date sdate = searchYeWu.getSdate();
        Date edate = searchYeWu.getEdate();
        System.out.println("eid:" + eid);
        if (eid != 0 || ename.trim().length() > 0 || sdate != null || edate != null) {
            if (eid != 0) {
                System.out.println("不为空:" + eid);
                hashmap.put("eid", "%" + eid + "%");
            }
            if (ename.trim().length() > 0) {
                hashmap.put("ename", "%" + ename + "%");
            }
            if (sdate != null) {
                hashmap.put("sdate", sdate);
            }
            if (edate != null) {
                hashmap.put("edate", edate);
            }

            List<Employee> list = employeeService.selectWorkloadByYear(hashmap);
            String[] name = new String[list.size()];
            Integer[] wcompleteness = new Integer[list.size()];
            Integer[] wdonereportload = new Integer[list.size()];
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < list.size(); i++) {
                name[i] = list.get(i).getEname();
                Set<WorkLoad> set = list.get(i).getSet();
                for (WorkLoad workLoad : set) {
                    wcompleteness[i] = workLoad.getWcompleteness();
                    wdonereportload[i] = workLoad.getWdonereportload();

                }
            }
            map.put("name", name);
            map.put("wcompleteness", wcompleteness);
            map.put("wdonereportload", wdonereportload);
            return map;
        } else {
            List<Employee> list = employeeService.selectWorkloadByYear();
            String[] name = new String[list.size()];
            Integer[] wcompleteness = new Integer[list.size()];
            Integer[] wdonereportload = new Integer[list.size()];
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < list.size(); i++) {
                name[i] = list.get(i).getEname();

                Set<WorkLoad> set = list.get(i).getSet();

                for (WorkLoad workLoad : set) {
                    wcompleteness[i] = workLoad.getWcompleteness();
                    wdonereportload[i] = workLoad.getWdonereportload();

                }
            }
            map.put("name", name);
            map.put("wcompleteness", wcompleteness);
            map.put("wdonereportload", wdonereportload);
            return map;
        }

    }
}
